<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="隧道名称" prop="tunnelName">
        <el-select v-model="queryParams.tunnelName" placeholder="请选择隧道名称" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in suidaoList"
            :key="item.id + ''"
            :label="item.field2"
            :value="item.field2 + ''"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型编码" prop="electricalEquipmentId">
        <el-input
          v-model="queryParams.electricalEquipmentId"
          placeholder="请输入设备类型编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:jidianpingdingfenxiang:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          v-if="false"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:jidianpingdingfenxiang:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:jidianpingdingfenxiang:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['biz:jidianpingdingfenxiang:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subentryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评定分项单号" align="center" prop="subentryId" />
      <el-table-column label="隧道名称" align="center" prop="tunnelName" />
      <el-table-column label="设备类型编码" align="center" prop="electricalEquipmentId" />
      <el-table-column label="设备完好率" align="center" prop="equipmentCompleteness" />
      <el-table-column label="权重" align="center" prop="weight" />
      <el-table-column label="上次评定日期" align="center" prop="lastRateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastRateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本次评定日期" align="center" prop="rateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评定人" align="center" prop="rater" />
      <el-table-column label="负责人" align="center" prop="recorder" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:jidianpingdingfenxiang:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:jidianpingdingfenxiang:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="隧道名称" prop="tunnelName">
          <el-select v-model="form.tunnelName" placeholder="请选择隧道名称">
            <el-option
              v-for="dict in suidaoList"
              :key="dict.id + ''"
              :label="dict.field2"
              :value="dict.field2 + ''"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型编码" prop="electricalEquipmentId">
          <el-input v-model="form.electricalEquipmentId" placeholder="请输入设备类型编码" />
        </el-form-item>
        <el-form-item label="评定人" prop="rater">
          <el-input v-model="form.rater" placeholder="请输入评定人" />
        </el-form-item>
        <el-form-item label="负责人" prop="recorder">
          <el-input v-model="form.recorder" placeholder="请输入负责人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    修改对话框-->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules2" label-width="80px">
        <el-form-item label="评定人" prop="rater">
          <el-input v-model="form.rater" placeholder="请输入评定人" />
        </el-form-item>
        <el-form-item label="负责人" prop="recorder">
          <el-input v-model="form.recorder" placeholder="请输入负责人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSubentry, getSubentry, delSubentry, addSubentry, updateSubentry, exportSubentry } from "@/api/biz/jidianpingdingfenxiang";
import { listSuidao } from "@/api/biz/suidao";

export default {
  name: "jidianpingdingfenxiang",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      suidaoList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      subentryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tunnelName: null,
        electricalEquipmentId: null,
        equipmentCompleteness: null,
        weight: null,
        lastRateTime: null,
        rateTime: null,
        rater: null,
        recorder: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules1: {
        tunnelName: [
          { required: true, message: "隧道名称不能为空", trigger: "blur" },
        ],
        electricalEquipmentId: [
          { required: true, message: "设备类型编码不能为空", trigger: "blur" },
        ],
        rater: [
          { required: true, message: "评定人不能为空", trigger: "blur" },
        ],
        recorder: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
      },
      rules2: {
        rater: [
          { required: true, message: "评定人不能为空", trigger: "blur" },
        ],
        recorder: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    listSuidao({}).then((response) => {
      this.suidaoList = response.rows;
    });
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listSubentry(this.queryParams).then(response => {
        this.subentryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        subentryId: null,
        tunnelName: null,
        electricalEquipmentId: null,
        equipmentCompleteness: null,
        weight: null,
        lastRateTime: null,
        rateTime: null,
        rater: null,
        recorder: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subentryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "添加技术状况评定分项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const subentryId = row.subentryId || this.ids
      getSubentry(subentryId).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改技术状况评定分项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.subentryId != null) {
            updateSubentry(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            addSubentry(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const subentryIds = row.subentryId || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + subentryIds + '"的数据项？').then(function() {
        return delSubentry(subentryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有设备评定分项记录数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportSubentry(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  }
};
</script>
